package com.example.demo1.generator.velocity;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URLEncoder;

public class HelloVelocity {
    public static void main(String[] args) throws IOException {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

        ve.init();
        Template helloTemplate = ve.getTemplate("templates/velocity/vue-router.vm");
        VelocityContext ctx = new VelocityContext();

//        ctx.put("name", "velocity");
//        ctx.put("date", (new Date()).toString());
//        List temp = new ArrayList();
//        temp.add("1");
//        temp.add("2");
//        ctx.put("list", temp);

        String tableName = "base_user";
        String tableName1 = "BaseUser";
        String module = tableName.substring(0, tableName.indexOf("_"));
        String entity = tableName.substring(tableName.indexOf("_") + 1);
        ctx.put("module", module);
        ctx.put("entity", entity);
        ctx.put("tableName", tableName1);

        StringWriter sw = new StringWriter();

        helloTemplate.merge(ctx, sw);

        String data = sw.toString();

        //写出；
//        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//        ZipOutputStream zip = new ZipOutputStream(outputStream);
//        ZipEntry zipEntry=new ZipEntry("Hello.vue");
//        zip.putNextEntry(zipEntry);
//        IOUtils.write(data, zip, "UTF-8");

        String basefilePath = "F:\\liangxin\\src\\";
//        String fileName = System.currentTimeMillis()+".zip";
        /**
         * router路由
         */
        String routerfilePath = basefilePath + "\\router\\";

        String fileName = entity+ ".js";
        File fileDir = new File(routerfilePath);
        if (!fileDir.exists()) { // 如果不存在 则创建
            fileDir.mkdirs();
        }
        URLEncoder.encode(fileName, "utf-8");
        FileOutputStream out = new FileOutputStream(routerfilePath + fileName);
        out.write(data.getBytes());
        out.flush();
        out.close();

        /**
         * vue-index
         */
        Template indexTemplate = ve.getTemplate("templates/velocity/vue-index.vm");
        sw = new StringWriter();
        indexTemplate.merge(ctx, sw);
        String indexdata = sw.toString();
        String indexFilePath = basefilePath + "\\view\\" + module + "\\" + entity + "\\";
        String indexFileName = "index.vue";
        fileDir = new File(indexFilePath);
        if (!fileDir.exists()) {
            // 如果不存在 则创建
            fileDir.mkdirs();
        }
        URLEncoder.encode(indexFileName, "utf-8");
        out = new FileOutputStream(indexFilePath + indexFileName);
        out.write(indexdata.getBytes());
        out.flush();
        out.close();

        /**
         * vue-edit
         */
        Template editTemplate = ve.getTemplate("templates/velocity/vue-edit.vm");
        sw = new StringWriter();
        editTemplate.merge(ctx, sw);
        String editdata = sw.toString();
        String editFilePath = basefilePath + "\\view\\" + module + "\\" + entity + "\\";
        String editFileName = "edit.vue";
        fileDir = new File(editFilePath);
        if (!fileDir.exists()) {
            // 如果不存在 则创建
            fileDir.mkdirs();
        }
        URLEncoder.encode(editFileName, "utf-8");
        out = new FileOutputStream(editFilePath + editFileName);
        out.write(editdata.getBytes());
        out.flush();
        out.close();

    }
}